package controllers

import dao._

import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import shared.VarTool
import tool.{FormTool, Tool}
import utils.Utils

import java.io.File
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
 * Created by yz on 2/4/2020
 */
class TestController @Inject()(cc: ControllerComponents)(
  implicit val modeDao: ModeDao,
  implicit val missionDao: MissionDao,
  implicit val sampleDao: SampleDao,
) extends AbstractController(cc) {

  def upToBigData = Action.async { implicit request =>
    sampleDao.selectAll(2).flatMap { curDatas =>
      val bigDatas = curDatas.flatMap { data =>
        (1 to 5).map { i =>
          data.copy(id = s"${data.id}_${i}", updateMission = s"${data.updateMission}_${i}")
        }
      }
      bigDatas.grouped(30000).foreach { datas =>
        val f = sampleDao.insertOrUpdates(datas)
        Utils.execFuture(f)
      }
      Future {}
    }.map { x =>
      Ok(Json.toJson("success!"))
    }
  }

  def test = Action.async { implicit request =>
    val userId=0
    val missionId=76
//    val userId=2
//    val missionId=37
    val workspaceDir = new File(s"C:\\forensic_database\\user\\${userId}\\mission\\${missionId}\\workspace")
    missionDao.select(missionId).map { mission =>
      Tool.resultDeal(workspaceDir, mission, sampleDao)
      Ok("success!")
    }

  }

  def stepSearch = Action.async { implicit request =>
    val startTime = System.currentTimeMillis()
    val siteNames = VarTool.yarnSiteNames
    val tmpDir = Tool.createTempDirectory("tmpDir")
    val userId = Tool.getUserId

    sampleDao.selectSiteInfoById(userId, "test-1").flatMap { case (id, map) =>

      val newMap = siteNames.map { siteName =>
        (siteName -> map.getOrElse(siteName, ""))
      }.toMap

      sampleDao.test(userId, newMap, 1).map { x =>
        println("===")
        println(x)
        x.foreach { y =>
          println(y)
        }
        Utils.logTime(startTime)
        Ok(Json.toJson("success!"))
      }

    }



    //    sampleDao.selectSiteInfoById(userId, data.ids.head).zip(sampleDao.selectAllSiteInfo(userId)).map { case (sample, dbSamples) =>
    //      Utils.logTime(startTime)
    //      val sampleSiteInfoJson = sample._2
    //      val sampleValues = siteNames.map { siteName =>
    //        val t = (sampleSiteInfoJson.getOrElse(siteName,""))
    //        t
    //      }
    //      Utils.logTime(startTime)
    //      val siteNum = siteNames.size
    //      var i = 0
    //      val filterSamples = dbSamples.filter { case (id, siteInfo) =>
    //        i += 1
    //        if (i % 1000 == 0) {
    //          Utils.logTime(s"${i}", startTime)
    //        }
    //        val siteInfoJson = siteInfo
    //
    //        val map = siteInfoJson.filter { case (siteName, siteJson) =>
    //          siteNames.contains(siteName)
    //        }
    //
    //        val values = siteNames.map { siteName =>
    //          map.getOrElse(siteName, "")
    //        }
    //        val valuesT = sampleValues.zip(values)
    //
    //        def loop(acc: List[(String, String)], remainValuesT: List[(String, String)]): List[(String, String)] = {
    //
    //          remainValuesT match {
    //            case Nil => acc
    //            case x :: xs =>
    //              if (acc.size >= 2) {
    //                loop(acc, List.empty)
    //              } else {
    //                val b = x._1.nonEmpty && x._2.nonEmpty && x._1 != x._2
    //                if (b) {
    //                  val curAcc = x :: acc
    //                  loop(curAcc, xs)
    //                } else {
    //                  loop(acc, xs)
    //                }
    //              }
    //          }
    //        }
    //
    //        val nonSampleValuesT = loop(List.empty, valuesT)
    //        nonSampleValuesT.size < 2
    //      }.map(_._1)
    //      Utils.logTime(startTime)
    //      println(filterSamples.size)
    //
    //      Ok(Json.toJson("success!"))
    //    }
  }


}
